/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package comunicacao;

import entities.Server;
import entities.Teatro;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;


/**
 *
  * @author Leonardo Lino Nº45340
 * @author Diogo Pires Nº40351
 */
public class RmiClient {

    MessageRedirectorInterface redirector;
    ReceiveMessageInterface dbServer;
    Registry registry;
    Server server;
    GestorZooKeeper gestorZK;
    Server primary;

    public RmiClient(GestorZooKeeper zk) {
       gestorZK = zk;
       primary = gestorZK.getPrimaryServer();

    }

    public boolean connectList() {
        try {

            comumPrimary(primary);
            String res = redirector.listTheaters();
             String[] pieces = res.split(":");
            Server server = new Server(pieces[0], Integer.parseInt(pieces[1]));
            server.setRmiBindName(pieces[2]);

            parteComum(server);
            ArrayList<String> lista = dbServer.listTheaters();
            
             if (lista==null)
                 return false;
             else
                 return true;
            
           
        } catch (RemoteException e) {
            System.err.println(e.toString());
            return false;
        }
    }

    public boolean connectSelect(String theaterName) {
         try {

            server = getCorrectDBServer(theaterName);

            if (!parteComum(server)) 
                return false;
             Teatro teatro = dbServer.getTheater(theaterName);
             
             if (teatro==null)
                 return false;
             else
                 return true;
            
        } catch (RemoteException e) {
            System.err.println(e.toString());
            return false;
        }
    }

    private Server getCorrectDBServer(String teatroNome) throws RemoteException {

        comumPrimary(primary);
        String res = redirector.getResponsible(teatroNome);

        String[] pieces = res.split(":");
        Server server = new Server(pieces[0], Integer.parseInt(pieces[1]));
        server.setRmiBindName(pieces[2]);

        return server;
    }
     
    
     private boolean comumPrimary(Server server) {
        try {
            registry = LocateRegistry.getRegistry(
                    server.getIp(), server.getPort());

            // look up the remote object
            redirector = (MessageRedirectorInterface) (registry.lookup(server.getRmiBindName()));

        } catch (RemoteException e) {
            e.printStackTrace();
            return false;
        } catch (NotBoundException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    private boolean parteComum(Server server) {

        try {
            registry = LocateRegistry.getRegistry(
                    server.getIp(), server.getPort());

            // look up the remote object
            dbServer = (ReceiveMessageInterface) (registry.lookup(server.getRmiBindName()));

        } catch (RemoteException e) {
            e.printStackTrace();
            return false;
        } catch (NotBoundException e) {
            e.printStackTrace();
            return false;
        }
        return true;

    }

    
}
